package com.begye.smartloan.apptest.donotleavepage

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import com.begye.smartloan.apptest.R
import com.begye.smartloan.apptest.databinding.AtDoNotLeavePageBinding
import com.begye.smartloan.apptest.ext.addColor
import com.begye.smartloan.apptest.ext.addCountdown
import com.begye.smartloan.apptest.gongong.YongHuJiDingYiViewModel
import com.begye.smartloan.apptest.kefu.KeFuDialogFragment
import com.begye.smartloan.apptest.outsuccess.OutSuccessActivity
import com.begye.smartloan.apptest.tool.LoginTool
import com.begye.smartloan.apptest.tool.addTouch
import com.data.datalib.ASSycManager
import com.util.utillib.extend.IntentFlagMode
import com.util.utillib.extend.toNextActivity


@SuppressLint("CustomWelcomeScreen")
class DoNotLeavePageActivity : AppCompatActivity() {

    private lateinit var _binding: AtDoNotLeavePageBinding

    private val _yongHuJiDingYiViewModel by lazy {
        ViewModelProvider(this)[YongHuJiDingYiViewModel::class.java]
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()

        _binding = AtDoNotLeavePageBinding.inflate(layoutInflater)
        setContentView(_binding.root)

        configYongHuLiuChengState()
        configUIState()
        configUIEvent()
        configUIData()
    }

    private fun configYongHuLiuChengState() {

        _yongHuJiDingYiViewModel.onLoadingStateLiveData.observe(this) {
            if (it == true) {
                _binding.atGlobalLoading.show()
            } else {
                _binding.atGlobalLoading.hide()
            }
        }

        _yongHuJiDingYiViewModel.onAnaDataFinishedDataLiveData.observe(this) {
            if (it) {
                toNextActivity(
                    activity = OutSuccessActivity::class.java,
                    mode = IntentFlagMode.SINGLE_TOP
                )
            }
        }
    }

    private fun configUIState() {

    }

    private fun configUIEvent() {

        _binding.atDoNotLeavePageNar.initConfig(
            text = "",
            leftResId = R.drawable.dres_39,
            rightResId = R.drawable.dres_84,
            hideLeftRes = true
        ) {
            if (0 == it) {
                //
            } else if (1 == it) {
                val dialog = KeFuDialogFragment()
                dialog.showNow(supportFragmentManager, "kefu")
            }
        }

        _binding.atDoNotLeavePageTips.addColor(
            getString(R.string.do_not_leave_page_text_3),
            "#FF0000"
        )

        _binding.atDoNotLeavePageCutdown.addCountdown(
            seconds = 10,
            onTick = { sec ->
                runOnUiThread {
                    if (8 == sec) {
                        _binding.atDoNotLeavePageStepIcon1.setImageResource(R.drawable.dres_35)
                        _binding.atDoNotLeavePageStepLine1.setBackgroundResource(R.drawable.layer_c2_4db673)

                        _binding.atDoNotLeavePageStepIcon2.setImageResource(R.drawable.dres_36)
                        _binding.atDoNotLeavePageStepLine2.setBackgroundResource(R.drawable.layer_c2_bbbbbb)

                        _binding.atDoNotLeavePageStepIcon3.setImageResource(R.drawable.dres_37)

                    } else if (5 == sec) {
                        _binding.atDoNotLeavePageStepIcon1.setImageResource(R.drawable.dres_35)
                        _binding.atDoNotLeavePageStepLine1.setBackgroundResource(R.drawable.layer_c2_4db673)

                        _binding.atDoNotLeavePageStepIcon2.setImageResource(R.drawable.dres_35)
                        _binding.atDoNotLeavePageStepLine2.setBackgroundResource(R.drawable.layer_c2_4db673)

                        _binding.atDoNotLeavePageStepIcon3.setImageResource(R.drawable.dres_36)

                    } else if (2 == sec) {
                        _binding.atDoNotLeavePageStepIcon1.setImageResource(R.drawable.dres_35)
                        _binding.atDoNotLeavePageStepLine1.setBackgroundResource(R.drawable.layer_c2_4db673)

                        _binding.atDoNotLeavePageStepIcon2.setImageResource(R.drawable.dres_35)
                        _binding.atDoNotLeavePageStepLine2.setBackgroundResource(R.drawable.layer_c2_4db673)

                        _binding.atDoNotLeavePageStepIcon3.setImageResource(R.drawable.dres_35)
                    }
                }
            },
            onFinished = {
                runOnUiThread {
                    _yongHuJiDingYiViewModel.requestSaveDataAnlFinished()
                }
            }
        )
    }

    private fun configUIData(){
        try {
            ASSycManager.syncData(LoginTool.filterSession()) {}
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun onResume() {
        super.onResume()

        addTouch(type = this.javaClass.simpleName, isOpen = true)
    }

    override fun onPause() {
        super.onPause()

        addTouch(type = this.javaClass.simpleName, isComplete = true)
    }
}

